home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROCS.ZIP / NGRAMS.ICN < prev    next >
Text File  |  1992-09-28  |  2KB  |  51 lines

  1. ############################################################################
  2. #
  3. #    File:     ngrams.icn
  4. #
  5. #    Subject:  Procedure to generate n-grams
  6. #
  7. #    Author:   Ralph E. Griswold
  8. #
  9. #    Date:     June 10, 1988
  10. #
  11. ###########################################################################
  12. #
  13. #     The procedure ngrams(file,n,c,t) generates a tabulation of the n-grams
  14. #  in the specified file.  If c is non-null, it is used as the set of
  15. #  characters from which n-grams are taken (other characters break n-grams).
  16. #  The default for c is the upper- and lowercase letters.  If t is non-null,
  17. #  the tabulation is given in order of frequency; otherwise in alphabetical
  18. #  order of n-grams.
  19. #
  20. #  Note:
  21. #
  22. #     The n-grams are kept in a table within the procedure and all n-grams
  23. #  are processed before the tabulation is generated. Consequently, this
  24. #  procedure is unsuitable if there are very many different n-grams.
  25. #
  26. ############################################################################
  27.  
  28. procedure ngrams(f,i,c,t)
  29.    local line, grams, a, count
  30.  
  31.    if not (integer(i) > 0) then stop("invalid ngrams specification")
  32.    if type(f) ~== ("file" | "window") then stop("invalid file specification")
  33.    /c := &lcase || &ucase
  34.    if not (c := cset(c)) then stop("invalid cset specification")
  35.    grams := table(0)
  36.    line := ""
  37.    while line ||:= reads(f,1000) do
  38.       line ? while tab(upto(c)) do
  39.          (tab(many(c)) \ 1) ? while grams[move(i)] +:= 1 do
  40.             move(-i + 1)
  41.    if /t then {
  42.       a := sort(grams,4)
  43.       while count := pull(a) do
  44.          suspend pull(a) || right(count,8)
  45.          }
  46.    else {
  47.       a := sort(grams,3)
  48.       suspend |(get(a) || right(get(a),8))
  49.       }
  50. end
  51.